package com.github.od.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.od.bean.ClientInfo;
import com.github.od.entity.Client;
import com.github.od.exception.auth.ClientInvalidException;
import com.github.od.mapper.ClientMapper;
import com.github.od.service.ClientService;

/**
* 数据库有关Service实现类
* 
* @author xieweikun
* @version 2017年11月19日 下午3:28:32
*/
@Service
public class DBClientService implements ClientService {
	
	@Autowired
	private ClientMapper clientMapper;

	@Override
	public ClientInfo apply(String clientId, String secret) {
		Client client = new Client();
		client.setCode(clientId);
		client = clientMapper.selectOne(client);
		if(client==null||!client.getSecret().equals(secret)) {
			throw new ClientInvalidException("Client not found or Client secret is error!");
		}
		return new ClientInfo(client.getCode(), client.getName(), client.getId().toString());
	}

	@Override
	public List<String> getAllowedClient(String clientId, String secret) {
		// 校验合法性
		ClientInfo info = this.apply(clientId, secret);
		return clientMapper.selectAllowedClient(info.getId());
	}

}
